package com.sun.designpatternsdemo.memento

import android.os.Bundle
import android.util.Log
import com.sun.designpatternsdemo.R
import com.sun.designpatternsdemo.app.AppConstant
import com.sun.designpatternsdemo.app.EMTagHandler
import com.sun.designpatternsdemo.base.BaseActivity
import com.sun.designpatternsdemo.databinding.ActivityMementoBinding

/**
 * 版权：xx公司 版权所有
 * @author sun
 * 版本：1.0
 * 创建日期：${DATE} ${TIME}
 * 描述：${DATA}
 * * 备忘录模式（Memento Pattern）保存一个对象的某个状态，以便在适当的时候恢复对象。备忘录模式属于行为型模式。
 * <p>
 * 主要解决：所谓备忘录模式就是在不破坏封装的前提下，捕获一个对象的内部状态，
 * 并在该对象之外保存这个状态，这样可以在以后将对象恢复到原先保存的状态。
 */
class MementoActivity : BaseActivity<ActivityMementoBinding>() {

    override fun layoutId(): Int =R.layout.activity_memento

    override fun initView(savedInstanceState: Bundle?) {
        title = "备忘录模式"
        mDataBind.apply {
            tvDefine.text = EMTagHandler.fromHtml(AppConstant.MEMENTO_DEFINE)
            btUseMemento.setOnClickListener {
                //管理者
                val careTaker = CareTaker()
                val originator = Originator("State #1")
                originator.state = "State #1"
                originator.state = "State #2"
                //保存状态
                careTaker.add(originator.setStateToMemento())

                originator.state = "State #3"
                //保存状态
                careTaker.add(originator.setStateToMemento())
                originator.state = "State #4"
                Log.e("---", "Current State: " + originator.state)
                //得到保存的状态
                val fromMemento1 = originator.getStateFromMemento(careTaker.get(0))
                Log.e("---", "First Saved State: $fromMemento1")
                val fromMemento2 = originator.getStateFromMemento(careTaker.get(1))
                Log.e("---", "Second Saved State: $fromMemento2")
            }
        }
    }
}